package com.demo.Controller;

import com.demo.entity.ExcelPo;
import com.demo.excel.ExcelImport;
import com.demo.service.ExcelService;
import com.demo.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author zqc
 * @Date 2021/04/01 10:28
 * @Description
 */
@RestController
@Slf4j
@Api(tags = "Excel操作")
@RequestMapping("/excelOperation")
public class ExcelReadAndWriteController {

    @Autowired
    private ExcelService excelService;

    @PostMapping("/readExcelData")
    @ApiOperation(value = "读取Excel内容", notes = "读取Excel内容")
    public ResultUtil readExcelData(MultipartFile file){
        //实体类接收Excel表格数据-枚举
        ExcelPo.Field[] fields = ExcelPo.Field.values();
        //接收Excel表格抬头数据-初始化
        String[] titles = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            titles[i] = fields[i].getField();
        }

        //定义读取文件的起始位置
        List<Map<String, String>> mapList = ExcelImport.readExcel(file, 0, 3, titles);
        //读取文件的所有name字段
        List<String> tempList = mapList.stream().map(it ->it.get("name")).collect(Collectors.toList());

        return ResultUtil.success(tempList);
    }

    @PostMapping("/exportStatisticsInfo")
    @ApiOperation("数据导出到Excel")
    public void exportStatisticsInfo(@RequestBody ExcelPo excelPo) throws Exception {
        excelService.exportExcelPoInfo(excelPo);
    }


}
